package com.cn.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@EnableScheduling
@Configuration
public class SendMessageJob
{

    //使用RestTemplate转发给前台
    @Autowired
    private RestTemplate restTemplate;

    @Scheduled(cron = "0/10 * * * * ?")
    public void sendMsg()
    {
        Map<String, String> map = new HashMap<>();
        Date date = new Date();
        map.put("message", "WebSocket发送当前时间为" + date.toString());
        //发送到controller中test方法中
        restTemplate.getForObject("http://localhost:8082/test?message={message}", Object.class, map);
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
